package com.wephone.Student.ui;

import java.io.File;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.text.format.Time;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.wephone.Student.R;
import com.wephone.Student.bean.EditMyInfoResponse;
import com.wephone.Student.bean.data.MyLocationInfo;
import com.wephone.Student.network.HttpClient;
import com.wephone.Student.network.MyResponseHandler;
import com.wephone.Student.util.Constants;
import com.wephone.Student.util.RoundImageView;
import com.wephone.Student.util.ShareReferenceUtil;
import com.wephone.Student.util.Util;

public class EditMyInfoActivity extends BaseActivity {
	private boolean isEditStatus = false;
	private int _year;
	private int _month;
	private int _day;
	private Time nowTime;
	private EditText userNameEdit;
	private TextView constellationsText;
	private TextView birthdayText;
	private RoundImageView userPhoto;
	private RadioButton genderRadio1;
	private RadioButton genderRadio2;
	private TextView detailEdit5;
	private TextView detailEdit6;
	private TextView detailEdit7;
	private EditText detailEdit8;
//	private File photoTempFile = new File(Constants.IMAGE_PHOTO_TMP_PATH+"tmpPic.jpg");
	private File photoSavedFile = null;
	private String photoSavedName = null;
//	private File subPicFile1 = new File(Constants.IMAGE_PHOTO_TMP_PATH+"subPic1.jpg");
	private File subSavedFile1 = null;
	private String subSavedName1 = null;
//	private File subPicFile2 = new File(Constants.IMAGE_PHOTO_TMP_PATH+"subPic2.jpg");
	private File subSavedFile2 = null;
	private String subSavedName2 = null;
//	private File subPicFile3 = new File(Constants.IMAGE_PHOTO_TMP_PATH+"subPic3.jpg");
	private File subSavedFile3 = null;
	private String subSavedName3 = null;
	private boolean hasPhotoUpload = false;
	private boolean hasSubPic1Changed = false;
	private boolean hasSubPic2Changed = false;
	private boolean hasSubPic3Changed = false;
	private ImageView subPic1;
	private ImageView subPic2;
	private ImageView subPic3;
	private ProgressDialog progressDialog;
	public static int uLocType=-1;
	public static String uLocDetail="";

	private MyResponseHandler getHandler(){
		MyResponseHandler handler = new MyResponseHandler(EditMyInfoActivity.this,EditMyInfoResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);

				if(progressDialog!=null){
					progressDialog.dismiss();
				}
				if(getDataSuccess){
					EditMyInfoResponse res = (EditMyInfoResponse)response;
					BaseActivity.infoData = res.getData();
					String infoJson = gson.toJson(res.getData());
					ShareReferenceUtil.saveUserInfoJson(getApplicationContext(), infoJson);
//					System.out.println("---server birth---"+res.getData().getuBirthday());
					Toast.makeText(getApplicationContext(), R.string.edit_my_info_commit_success, Toast.LENGTH_SHORT).show();
					setResult(11);
					finish();
					if(photoSavedFile!=null && photoSavedFile.exists()){
						photoSavedFile.delete();
					}
					if(subSavedFile1!=null && subSavedFile1.exists()){
						subSavedFile1.delete();
					}
					if(subSavedFile2!=null && subSavedFile2.exists()){
						subSavedFile2.delete();
					}
					if(subSavedFile3!=null && subSavedFile3.exists()){
						subSavedFile3.delete();
					}
//					imageLoader.clearDiscCache();
				}
			}
		};
		return handler;
	}
	
//	private Handler handler = new Handler(){
//		@Override
//		public void handleMessage(Message msg) {
//			if(msg.what==11){
//				List<CountryData> results = (List<CountryData>)msg.obj;
//				RegisterActivity.countryList.clear();
//				RegisterActivity.countryList.addAll(results);
//			}
//		}
//	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_my_info);
		
		init();
	}
	
	private void init(){
		nowTime = new Time("GMT+8");
		nowTime.setToNow();
		
		uLocType = Integer.parseInt(getMyInfo(getApplicationContext()).getuLocType());
		uLocDetail = getMyInfo(getApplicationContext()).getuLocDetail();
		
//		if(RegisterActivity.countryList.size()==0){
//			new GetCountryListAsyncTask(getApplicationContext(), handler).execute();
//		}
//		_year = nowTime.year;
//		_month = nowTime.month;
//		_day = nowTime.monthDay;
		
		userNameEdit = (EditText)findViewById(R.id.user_info_detail_edit_1);
		constellationsText = (TextView)findViewById(R.id.user_info_detail_edit_4);
		birthdayText = (TextView)findViewById(R.id.user_info_detail_edit_3);
		userPhoto = (RoundImageView)findViewById(R.id.user_info_detail_user_logo);
		genderRadio1 = (RadioButton)findViewById(R.id.gender_radio_1);
		genderRadio2 = (RadioButton)findViewById(R.id.gender_radio_2);
		detailEdit5 = (TextView)findViewById(R.id.user_info_detail_edit_5);
		detailEdit6 = (TextView)findViewById(R.id.user_info_detail_edit_6);
		detailEdit7 = (TextView)findViewById(R.id.user_info_detail_edit_7);
		detailEdit8 = (EditText)findViewById(R.id.user_info_detail_edit_8);
		subPic1 = (ImageView)findViewById(R.id.user_info_detail_pic_1);
		subPic2 = (ImageView)findViewById(R.id.user_info_detail_pic_2);
		subPic3 = (ImageView)findViewById(R.id.user_info_detail_pic_3);
		
		findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		findViewById(R.id.btn_finish).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(isEditStatus){
					isEditStatus = false;
					if(checkInput()){
						progressDialog = ProgressDialog.show(EditMyInfoActivity.this, "提示", "正在提交数据...", true, true);
						
						RequestParams params = new RequestParams();
						params.put("uId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
						params.put("uName", userNameEdit.getText().toString());
//						params.put("uEmail", BaseActivity.myInfo.getuEmail());
						if(!TextUtils.isEmpty(birthdayText.getText().toString())){
							try{
								SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
								Date date = sdf.parse(_year+"-"+(_month+1)+"-"+_day);
//								System.out.println("---edit birth---"+""+date.getTime()/1000);
								params.put("uBirthday", ""+date.getTime()/1000);
//								System.out.println("------nowTime.year-_year------"+(nowTime.year-_year-1));
								params.put("uAge", ""+(nowTime.year-_year-1));
								params.put("uConstellation", constellationsText.getText().toString());
							}catch(Exception e){
								e.printStackTrace();
							}
						}else{
							params.put("uBirthday", BaseActivity.getMyInfo(getApplicationContext()).getuBirthday());
							params.put("uAge", BaseActivity.getMyInfo(getApplicationContext()).getuAge());
							params.put("uConstellation", BaseActivity.getMyInfo(getApplicationContext()).getuConstellation());
						}
//						System.out.println("---genderRadio1.isChecked()---"+genderRadio1.isChecked());
						if(genderRadio1.isChecked()){
//							System.out.println("-----------0----------");
							params.put("uGender", "0");
						}else{
//							System.out.println("-----------1----------");
							params.put("uGender", "1");
						}
						
//						if(!TextUtils.isEmpty(detailEdit8.getText().toString())){
							params.put("uSign", detailEdit8.getText().toString());
//						}else{
//							params.put("uSign", "暂未编辑签名");
//						}
						if(hasPhotoUpload && photoSavedFile!=null && photoSavedFile.exists()){
							try {
								params.put("uIcon", photoSavedFile);
							} catch (FileNotFoundException e) {
								e.printStackTrace();
							}
						}else{
							params.put("uIcon", BaseActivity.getMyInfo(getApplicationContext()).getuIcon());
						}
//						params.put("uPlatform", BaseActivity.myInfo.getuPlatform());
//						params.put("uPlatformid", BaseActivity.myInfo.getuPlatformid());
						params.put("uGpsX", ShareReferenceUtil.getLocationLatitude(getApplicationContext()));
						params.put("uGpsY", ShareReferenceUtil.getLocationLontitude(getApplicationContext()));
						if(uLocType>0 && !TextUtils.isEmpty(uLocDetail)){
							params.put("uLocType", ""+uLocType);
							params.put("uLocDetail", uLocDetail);
						}else if(!TextUtils.isEmpty(BaseActivity.getMyInfo(getApplicationContext()).getuLocDetail())){
							params.put("uLocType", BaseActivity.getMyInfo(getApplicationContext()).getuLocType());
							params.put("uLocDetail", BaseActivity.getMyInfo(getApplicationContext()).getuLocDetail());
						}
//						if(){
//							Gson gson = new Gson();
//							MyLocationInfo locInfo = gson.fromJson(BaseActivity.myInfo.getuLocDetail(), MyLocationInfo.class);
//							if(locInfo.getType().equals("PlanGoOut")){
//								locInfo.setSchool(detailEdit7.getText().toString());
//							}else if(locInfo.getType().equals("Returnees")){
//								String[] schoolArray = detailEdit7.getText().toString().split(",");
//								if(schoolArray.length>0){
//									locInfo.setSchool1(schoolArray[0]);
//								}
//								if(schoolArray.length>1){
//									locInfo.setSchool1(schoolArray[1]);
//								}
//							}else{
//								locInfo.setSchool(detailEdit7.getText().toString());
//							}
//							locInfo.setCity(detailEdit5.getText().toString());
//							locInfo.setHome(detailEdit6.getText().toString());
//							params.put("uLocDetail", gson.toJson(locInfo));
//						}
						if(hasSubPic1Changed && subSavedFile1!=null && subSavedFile1.exists()){
							try {
								params.put("uImg1", subSavedFile1);
							} catch (FileNotFoundException e) {
								e.printStackTrace();
							}
						}else{
							params.put("uImg1", BaseActivity.getMyInfo(getApplicationContext()).getuImg1());
						}
						if(hasSubPic2Changed && subSavedFile2!=null && subSavedFile2.exists()){
							try {
								params.put("uImg2", subSavedFile2);
							} catch (FileNotFoundException e) {
								e.printStackTrace();
							}
						}else{
							params.put("uImg2", BaseActivity.getMyInfo(getApplicationContext()).getuImg2());
						}
						if(hasSubPic3Changed && subSavedFile3!=null && subSavedFile3.exists()){
							try {
								params.put("uImg3", subSavedFile3);
							} catch (FileNotFoundException e) {
								e.printStackTrace();
							}
						}else{
							params.put("uImg3", BaseActivity.getMyInfo(getApplicationContext()).getuImg3());
						}
						HttpClient.getInstance().post(Constants.API_USER_EDIT_USER_URL, params, getHandler());
					}
				}else{
					isEditStatus = true;
					((Button)findViewById(R.id.btn_finish)).setBackgroundResource(R.drawable.btn_ok_selector);
					//TODO
					userNameEdit.setEnabled(true);
					userPhoto.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							new AlertDialog.Builder(EditMyInfoActivity.this).setTitle("选择头像")
							.setItems(new String[]{"拍照","相册","还原"}, new DialogInterface.OnClickListener(){
								@Override
								public void onClick(DialogInterface arg0, int arg1) {
									if(arg1==0){
										File rootFile = new File(Constants.IMAGE_PHOTO_TMP_PATH);
										if(!rootFile.exists()){
											rootFile.mkdirs();
										}
										photoSavedName = "userlogo_"+System.currentTimeMillis()+".jpg";
										photoSavedFile = new File(Constants.IMAGE_PHOTO_TMP_PATH+photoSavedName);
										Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
										intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photoSavedFile));
					                    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
					                    startActivityForResult(intent, 1);
									}else if(arg1==1){
										photoSavedName = "userlogo_"+System.currentTimeMillis()+".jpg";
										photoSavedFile = new File(Constants.IMAGE_PHOTO_TMP_PATH+photoSavedName);
										Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
										startActivityForResult(i, 2);
									}else{
										if(photoSavedFile.exists()){
											photoSavedFile.delete();
										}
										photoSavedFile=null;
										userPhoto.setImageResource(R.drawable.default_add_picture);
									}
								}})
							.setNegativeButton("取消", null).show();
						}
					});
					genderRadio1.setEnabled(true);
					genderRadio2.setEnabled(true);
					findViewById(R.id.choose_date_layout).setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							showChooseDateDialog();
						}
					});
					detailEdit5.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							startActivityForResult(new Intent(getApplicationContext(),SelectUserLocationsActivity.class),111);
						}
					});
					detailEdit6.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							startActivityForResult(new Intent(getApplicationContext(),SelectUserLocationsActivity.class),111);
						}
					});
					detailEdit7.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							startActivityForResult(new Intent(getApplicationContext(),SelectUserLocationsActivity.class),111);
						}
					});
//					detailEdit5.setEnabled(true);
//					detailEdit6.setEnabled(true);
//					detailEdit7.setEnabled(true);
					detailEdit8.setEnabled(true);
					subPic1.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							new AlertDialog.Builder(EditMyInfoActivity.this).setTitle("添加图片")
							.setItems(new String[]{"拍照","相册"}, new DialogInterface.OnClickListener(){
								@Override
								public void onClick(DialogInterface arg0, int arg1) {
									if(arg1==0){
										File rootFile = new File(Constants.IMAGE_PHOTO_TMP_PATH);
										if(!rootFile.exists()){
											rootFile.mkdirs();
										}
										subSavedName1 = "usersub_"+System.currentTimeMillis()+".jpg";
										subSavedFile1 = new File(Constants.IMAGE_PHOTO_TMP_PATH+subSavedName1);
										Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
										intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(subSavedFile1));
					                    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
					                    startActivityForResult(intent, 101);
									}else if(arg1==1){
										subSavedName1 = "usersub_"+System.currentTimeMillis()+".jpg";
										subSavedFile1 = new File(Constants.IMAGE_PHOTO_TMP_PATH+subSavedName1);
										Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
										startActivityForResult(i, 102);
									}else{
										if(subSavedFile1.exists()){
											subSavedFile1.delete();
										}
										subSavedFile1=null;
										subPic1.setImageResource(R.drawable.default_add_picture);
									}
								}})
							.setNegativeButton("取消", null).show();
						}
					});
					subPic2.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							new AlertDialog.Builder(EditMyInfoActivity.this).setTitle("添加图片")
							.setItems(new String[]{"拍照","相册"}, new DialogInterface.OnClickListener(){
								@Override
								public void onClick(DialogInterface arg0, int arg1) {
									if(arg1==0){
										File rootFile = new File(Constants.IMAGE_PHOTO_TMP_PATH);
										if(!rootFile.exists()){
											rootFile.mkdirs();
										}
										subSavedName2 = "usersub_"+System.currentTimeMillis()+".jpg";
										subSavedFile2 = new File(Constants.IMAGE_PHOTO_TMP_PATH+subSavedName2);
										Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
										intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(subSavedFile2));
					                    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
					                    startActivityForResult(intent, 201);
									}else if(arg1==1){
										subSavedName2 = "usersub_"+System.currentTimeMillis()+".jpg";
										subSavedFile2 = new File(Constants.IMAGE_PHOTO_TMP_PATH+subSavedName2);
										Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
										startActivityForResult(i, 202);
									}else{
										if(subSavedFile2.exists()){
											subSavedFile2.delete();
										}
										subSavedFile2=null;
										subPic2.setImageResource(R.drawable.default_add_picture);
									}
								}})
							.setNegativeButton("取消", null).show();
						}
					});
					subPic3.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							new AlertDialog.Builder(EditMyInfoActivity.this).setTitle("添加图片")
							.setItems(new String[]{"拍照","相册"}, new DialogInterface.OnClickListener(){
								@Override
								public void onClick(DialogInterface arg0, int arg1) {
									if(arg1==0){
										File rootFile = new File(Constants.IMAGE_PHOTO_TMP_PATH);
										if(!rootFile.exists()){
											rootFile.mkdirs();
										}
										subSavedName3 = "usersub_"+System.currentTimeMillis()+".jpg";
										subSavedFile3 = new File(Constants.IMAGE_PHOTO_TMP_PATH+subSavedName3);
										Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
										intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(subSavedFile3));
					                    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
					                    startActivityForResult(intent, 301);
									}else if(arg1==1){
										subSavedName3 = "usersub_"+System.currentTimeMillis()+".jpg";
										subSavedFile3 = new File(Constants.IMAGE_PHOTO_TMP_PATH+subSavedName3);
										Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
										startActivityForResult(i, 302);
									}else{
										if(subSavedFile3.exists()){
											subSavedFile3.delete();
										}
										subSavedFile3=null;
										subPic3.setImageResource(R.drawable.default_add_picture);
									}
								}})
							.setNegativeButton("取消", null).show();
						}
					});
				}
			}
		});
		//点击图片大图显示
		subPic1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showBigPictureDialog(getApplicationContext(), 0);
			}
		});
		subPic2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showBigPictureDialog(getApplicationContext(), 1);
			}
		});
		subPic3.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showBigPictureDialog(getApplicationContext(), 2);
			}
		});
		
		userNameEdit.setText(BaseActivity.getMyInfo(getApplicationContext()).getuName());
		constellationsText.setText(BaseActivity.getMyInfo(getApplicationContext()).getuConstellation());
//		System.out.println("---birth---"+BaseActivity.myInfo.getuBirthday());
		if(BaseActivity.getMyInfo(getApplicationContext()).getuBirthday().contains("-")){
			try {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				Date date = sdf.parse(BaseActivity.getMyInfo(getApplicationContext()).getuBirthday());
				Calendar c = Calendar.getInstance();
				c.setTime(date);
				_year = c.get(Calendar.YEAR);
				_month = c.get(Calendar.MONTH);
				_day = c.get(Calendar.DAY_OF_MONTH);
//				System.out.println("_year:"+_year+" _month:"+_month+" _day:"+_day);
				SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日");
				birthdayText.setText(sdf2.format(date));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			String birthStr = BaseActivity.getMyInfo(getApplicationContext()).getuBirthday();
			int birthLength = birthStr.length();
			for(int i=0;i<(12-birthLength);i++){
				birthStr = birthStr+"0";
			}
			Date userDate = new Date(Long.parseLong(birthStr));
			Calendar c = Calendar.getInstance();
			c.setTime(userDate);
			_year = c.get(Calendar.YEAR);
			_month = c.get(Calendar.MONTH);
			_day = c.get(Calendar.DAY_OF_MONTH);
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日");
			birthdayText.setText(sdf2.format(userDate));
		}
		if(!TextUtils.isEmpty(BaseActivity.getMyInfo(getApplicationContext()).getuIcon())){
			DisplayImageOptions options = new DisplayImageOptions.Builder()
	        .showStubImage(R.drawable.default_user_logo)
	        .showImageOnFail(R.drawable.default_user_logo)
	        .bitmapConfig(Bitmap.Config.ARGB_8888)
	        .cacheInMemory()
	        .cacheOnDisc()
//	        .displayer(new RoundedBitmapDisplayer(1000))
	        .build();
//			imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+BaseActivity.getMyInfo(getApplicationContext()).getuIcon(), userPhoto, options);
			String endStr = BaseActivity.getMyInfo(getApplicationContext()).getuIcon().substring(BaseActivity.getMyInfo(getApplicationContext()).getuIcon().lastIndexOf("."));
			imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+BaseActivity.getMyInfo(getApplicationContext()).getuIcon().replace(endStr, "_150_150"+endStr), userPhoto, options);
		}
//		System.out.println("------BaseActivity.myInfo.getuGender()-----"+BaseActivity.myInfo.getuGender());
		if(BaseActivity.getMyInfo(getApplicationContext()).getuGender().equals("0")){
			genderRadio1.setChecked(true);
		}else{
			genderRadio2.setChecked(true);
		}
		if(!TextUtils.isEmpty(getMyInfo(getApplicationContext()).getuLocDetail())){
			Gson gson = new Gson();
			MyLocationInfo locInfo = gson.fromJson(getMyInfo(getApplicationContext()).getuLocDetail(), MyLocationInfo.class);
			if(locInfo.getType().equals("PlanGoOut")){
				detailEdit7.setText("想去"+locInfo.getToschool());
				detailEdit5.setText("中国 "+locInfo.getCity());
			}else if(locInfo.getType().equals("Returnees")){
				if(!TextUtils.isEmpty(locInfo.getSchool2())){
					detailEdit7.setText("曾在"+locInfo.getSchool1()+","+locInfo.getSchool2());
				}else{
					detailEdit7.setText("曾在"+locInfo.getSchool1());
				}
				detailEdit5.setText(locInfo.getLoc());
			}else{
				detailEdit7.setText(locInfo.getSchool());
				detailEdit5.setText(locInfo.getCity()+" "+locInfo.getCountry());
			}
//			detailEdit5.setText(locInfo.getCity());
			detailEdit6.setText(locInfo.getHome());
		}
		detailEdit8.setText(BaseActivity.getMyInfo(getApplicationContext()).getuSign());
		if(!TextUtils.isEmpty(BaseActivity.getMyInfo(getApplicationContext()).getuImg1())){
			imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+getMyInfo(getApplicationContext()).getuImg1(), subPic1);
//			String endStr = BaseActivity.getMyInfo(getApplicationContext()).getuImg1().substring(BaseActivity.getMyInfo(getApplicationContext()).getuImg1().lastIndexOf("."));
//			imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+BaseActivity.getMyInfo(getApplicationContext()).getuImg1().replace(endStr, "_150_150"+endStr),subPic1);
		}
		if(!TextUtils.isEmpty(BaseActivity.getMyInfo(getApplicationContext()).getuImg2())){
			imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+getMyInfo(getApplicationContext()).getuImg2(), subPic2);
//			String endStr = BaseActivity.getMyInfo(getApplicationContext()).getuImg2().substring(BaseActivity.getMyInfo(getApplicationContext()).getuImg2().lastIndexOf("."));
//			imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+BaseActivity.getMyInfo(getApplicationContext()).getuImg2().replace(endStr, "_150_150"+endStr),subPic2);
		}
		if(!TextUtils.isEmpty(BaseActivity.getMyInfo(getApplicationContext()).getuImg3())){
			imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+getMyInfo(getApplicationContext()).getuImg3(), subPic3);
//			String endStr = BaseActivity.getMyInfo(getApplicationContext()).getuImg3().substring(BaseActivity.getMyInfo(getApplicationContext()).getuImg3().lastIndexOf("."));
//			imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+BaseActivity.getMyInfo(getApplicationContext()).getuImg3().replace(endStr, "_150_150"+endStr),subPic3);
		}
	}

	private boolean checkInput(){
		if(TextUtils.isEmpty(userNameEdit.getText().toString().trim())){
			Toast.makeText(getApplicationContext(), "昵称必须填写", Toast.LENGTH_SHORT).show();
			return false;
		}
//		else if(TextUtils.isEmpty(detailEdit8.getText().toString())){
//			Toast.makeText(getApplicationContext(), "标题必须填写", Toast.LENGTH_SHORT).show();
//			return false;
//		}
		return true;
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if(resultCode==111){
			//TODO
			if(!TextUtils.isEmpty(uLocDetail)){
				Gson gson = new Gson();
				MyLocationInfo locInfo = gson.fromJson(uLocDetail, MyLocationInfo.class);
				if(uLocType==2){
					detailEdit7.setText("想去"+locInfo.getToschool());
				}else if(uLocType==1){
					if(!TextUtils.isEmpty(locInfo.getSchool2())){
						detailEdit7.setText("曾在"+locInfo.getSchool1()+","+locInfo.getSchool2());
					}else{
						detailEdit7.setText("曾在"+locInfo.getSchool1());
					}
				}else{
					detailEdit7.setText(locInfo.getSchool());
				}
				detailEdit5.setText(locInfo.getCity());
				detailEdit6.setText(locInfo.getHome());
			}
		}else if(requestCode==1){
			if(photoSavedFile!=null && photoSavedFile.exists()){
				hasPhotoUpload = true;
//				Bitmap bitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(photoSavedFile.getPath()), 300, 300);
				Bitmap bitmap = Util.getBitmap(getApplicationContext(),photoSavedFile.getPath(),300);
       		 	userPhoto.setImageBitmap(bitmap);
       		 	Util.saveMyBitmap(bitmap,photoSavedName,getApplicationContext());
			}else if(data!=null){
				Bundle bundle = data.getExtras();
				if(bundle!=null){
					Bitmap tmpbitmap = (Bitmap)bundle.get("data");
					if(tmpbitmap!=null){
						hasPhotoUpload = true;
						userPhoto.setImageBitmap(tmpbitmap);
		       		 	Util.saveMyBitmap(tmpbitmap,photoSavedName,getApplicationContext());
					}
				}
			}
   	 	}else if(requestCode==2){
	   	 	if(data!=null){
		   	 	Uri selectedImage = data.getData();
		        String[] filePathColumn = {MediaStore.Images.Media.DATA};
		        Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
		        cursor.moveToFirst();
		        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
		        String picturePath = cursor.getString(columnIndex);
		        cursor.close();
		        if(!TextUtils.isEmpty(picturePath)){
		        	 File userLogoFile = new File(picturePath);  
		        	 if(userLogoFile.exists()){
		        		 hasPhotoUpload = true;
//		        		 Bitmap bitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(picturePath), 300, 300);
		        		 Bitmap bitmap = Util.getBitmap(getApplicationContext(),picturePath,300);
		        		 userPhoto.setImageBitmap(bitmap);
		        		 Util.saveMyBitmap(bitmap,photoSavedName,getApplicationContext());
		        	 }
		         }
	   	 	}
   	 	}else if(requestCode==101){
			if(subSavedFile1!=null && subSavedFile1.exists()){
				hasSubPic1Changed = true;
//				Bitmap bitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(subSavedFile1.getPath()), 300, 300);
				Bitmap bitmap = Util.getBitmap(getApplicationContext(),subSavedFile1.getPath(),300);
				subPic1.setImageBitmap(bitmap);
       		 	Util.saveMyBitmap(bitmap,subSavedName1,getApplicationContext());
			}else if(data!=null){
				Bundle bundle = data.getExtras();
				if(bundle!=null){
					Bitmap tmpbitmap = (Bitmap)bundle.get("data");
					if(tmpbitmap!=null){
						hasSubPic1Changed = true;
						subPic1.setImageBitmap(tmpbitmap);
		       		 	Util.saveMyBitmap(tmpbitmap,subSavedName1,getApplicationContext());
					}
				}
			}
   	 	}else if(requestCode==102){
	   	 	if(data!=null){
		   	 	Uri selectedImage = data.getData();
		        String[] filePathColumn = {MediaStore.Images.Media.DATA};
		        Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
		        cursor.moveToFirst();
		        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
		        String picturePath = cursor.getString(columnIndex);
		        cursor.close();
		        if(!TextUtils.isEmpty(picturePath)){
		        	 File userLogoFile = new File(picturePath);  
		        	 if(userLogoFile.exists()){
		        		 hasSubPic1Changed = true;
//		        		 Bitmap bitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(picturePath), 300, 300);
		        		 Bitmap bitmap = Util.getBitmap(getApplicationContext(),picturePath,300);
		        		 subPic1.setImageBitmap(bitmap);
		        		 Util.saveMyBitmap(bitmap,subSavedName1,getApplicationContext());
		        	 }
		         }
	   	 	}
   	 	}else if(requestCode==201){
			if(subSavedFile2!=null && subSavedFile2.exists()){
				hasSubPic2Changed = true;
//				Bitmap bitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(subSavedFile2.getPath()), 300, 300);
				Bitmap bitmap = Util.getBitmap(getApplicationContext(),subSavedFile2.getPath(),300);
				subPic2.setImageBitmap(bitmap);
       		 	Util.saveMyBitmap(bitmap,subSavedName2,getApplicationContext());
			}else if(data!=null){
				Bundle bundle = data.getExtras();
				if(bundle!=null){
					Bitmap tmpbitmap = (Bitmap)bundle.get("data");
					if(tmpbitmap!=null){
						hasSubPic2Changed = true;
						subPic2.setImageBitmap(tmpbitmap);
		       		 	Util.saveMyBitmap(tmpbitmap,subSavedName2,getApplicationContext());
					}
				}
			}
   	 	}else if(requestCode==202){
	   	 	if(data!=null){
		   	 	Uri selectedImage = data.getData();
		        String[] filePathColumn = {MediaStore.Images.Media.DATA};
		        Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
		        cursor.moveToFirst();
		        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
		        String picturePath = cursor.getString(columnIndex);
		        cursor.close();
		        if(!TextUtils.isEmpty(picturePath)){
		        	 File userLogoFile = new File(picturePath);  
		        	 if(userLogoFile.exists()){
		        		 hasSubPic2Changed = true;
//		        		 Bitmap bitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(picturePath), 300, 300);
		        		 Bitmap bitmap = Util.getBitmap(getApplicationContext(),picturePath,300);
		        		 subPic2.setImageBitmap(bitmap);
		        		 Util.saveMyBitmap(bitmap,subSavedName2,getApplicationContext());
		        	 }
		         }
	   	 	}
   	 	}else if(requestCode==301){
			if(subSavedFile3!=null && subSavedFile3.exists()){
				hasSubPic3Changed = true;
//				Bitmap bitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(subSavedFile3.getPath()), 300, 300);
				Bitmap bitmap = Util.getBitmap(getApplicationContext(),subSavedFile3.getPath(),300);
				subPic3.setImageBitmap(bitmap);
       		 	Util.saveMyBitmap(bitmap,subSavedName3,getApplicationContext());
			}else if(data!=null){
				Bundle bundle = data.getExtras();
				if(bundle!=null){
					Bitmap tmpbitmap = (Bitmap)bundle.get("data");
					if(tmpbitmap!=null){
						hasSubPic3Changed = true;
						subPic3.setImageBitmap(tmpbitmap);
		       		 	Util.saveMyBitmap(tmpbitmap,subSavedName3,getApplicationContext());
					}
				}
			}
   	 	}else if(requestCode==302){
   	 		if(data!=null){
	   	 		Uri selectedImage = data.getData();
		        String[] filePathColumn = {MediaStore.Images.Media.DATA};
		        Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
		        cursor.moveToFirst();
		        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
		        String picturePath = cursor.getString(columnIndex);
		        cursor.close();
		        if(!TextUtils.isEmpty(picturePath)){
		        	 File userLogoFile = new File(picturePath);  
		        	 if(userLogoFile.exists()){
		        		 hasSubPic3Changed = true;
//		        		 Bitmap bitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(picturePath), 300, 300);
		        		 Bitmap bitmap = Util.getBitmap(getApplicationContext(),picturePath,300);
		        		 subPic3.setImageBitmap(bitmap);
		        		 Util.saveMyBitmap(bitmap,subSavedName3,getApplicationContext());
		        	 }
		        }
   	 		}
   	 	}
	}

	private void showChooseDateDialog(){
		new DatePickerDialog(this, new OnDateSetListener() {
            public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
                _year = year;
                _month = monthOfYear;
                _day = dayOfMonth;
                birthdayText.setText(_year+"年"+(_month+1)+"月"+_day+"日");
                constellationsText.setText(Util.getConstellationStr(getApplicationContext(), _month+1, _day));
            }

        }, _year, _month, _day).show();
	}
	
	private void showBigPictureDialog(Context context,final int idx){
		final View inputView = getLayoutInflater().inflate(R.layout.dialog_show_big_picture_view, null);  
		ImageView bigImage = (ImageView)inputView.findViewById(R.id.item_big_image);
		if(idx==0){
			if(!TextUtils.isEmpty(getMyInfo(getApplicationContext()).getuImg1())){
				imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+getMyInfo(getApplicationContext()).getuImg1(), bigImage);
			}else{
				return;
			}
		}else if(idx==1){
			if(!TextUtils.isEmpty(getMyInfo(getApplicationContext()).getuImg2())){
				imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+getMyInfo(getApplicationContext()).getuImg2(), bigImage);
			}else{
				return;
			}
		}else if(idx==2){
			if(!TextUtils.isEmpty(getMyInfo(getApplicationContext()).getuImg3())){
				imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+getMyInfo(getApplicationContext()).getuImg3(), bigImage);
			}else{
				return;
			}
		}
		AlertDialog.Builder builder = new AlertDialog.Builder(EditMyInfoActivity.this);
		builder.setView(inputView);
		builder.setPositiveButton("保存图片", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				String bigImgUrl = "";
				if(idx==0){
					bigImgUrl = Constants.IMAGE_PHOTO_SERVER_PATH+getMyInfo(getApplicationContext()).getuImg1();
				}else if(idx==1){
					bigImgUrl = Constants.IMAGE_PHOTO_SERVER_PATH+getMyInfo(getApplicationContext()).getuImg2();
				}else if(idx==2){
					bigImgUrl = Constants.IMAGE_PHOTO_SERVER_PATH+getMyInfo(getApplicationContext()).getuImg3();
				}
				imageLoader.loadImage(bigImgUrl, new ImageLoadingListener() {
					@Override
					public void onLoadingStarted(String arg0, View arg1) {
					}
					
					@Override
					public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
					}
					
					@Override
					public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
						String savePath = Util.saveBitmapToGallery(arg2, "save_"+System.currentTimeMillis()/1000+".png");
						Toast.makeText(getApplicationContext(), "保存路径"+savePath, Toast.LENGTH_LONG).show();
					}
					
					@Override
					public void onLoadingCancelled(String arg0, View arg1) {
					}
				});
			}
		});
		builder.show();  
	}
	
}
